#include "Example_Mass_Spring_Simluation_Recording.h"

/* Qt Includes
#include <QGridLayout>
 */
/* Sx Library Includes 
#include <sxOpenGL_DefaultCameraRenderer.h>
#include <sxMassSpringSimulationController.h>
#include <sxOpenGL_DefaultMassSpringMeshRenderer.h>
#include <sxSelfCCD_MSM_CollisionDetectionHandler.h>
#include <sxMSM_TS_CollisionModel.h>
#include <sxMSM_TS_InverseVelocityResponseHandler.h>
#include <sxOBJ_VegaMassSpringMeshStorage.h>
#include <sxVega_TetgenMassSpringMeshStorage.h>
#include <sxCollisionModel.h>
#include <sxDiscreteCollisionModel.h>
#include <sxTriangleSoupCollisionModel.h>
#include <sxMassSpringMesh.h>
#include <sxMassSpringSimulation.h>
#include <sxInverseVelocityResponseHandler.h>
#include <sxEulerCollisionEventPoolAllocator.h>
*/
/* Sx Library Namespace reductions 
using Sx::Simulation::Simulations::MassSpringSimulation;
using Sx::Simulation::Collision::Models::MSM_TS_CollisionModel;
using Sx::Simulation::Objects::MassSpringMesh;
using Sx::Physics::Collision::Event::EulerCollisionEventPoolAllocator;
using Sx::SelfCCD::Handlers::Detection::SelfCCD_MSM_CollisionDetectionHandler;
using Sx::Simulation::Collision::Handlers::Response::MSM_TS_InverseVelocityResponseHandler;
using Sx::Simulation::Interface::MassSpringSimulationController;
using Sx::Simulation::Interface::MSS_ReplayController;
using Sx::OpenGL::Graphics::Objects::Rendering::OpenGL_DefaultCameraRenderer;
*/

Example_Mass_Spring_Simluation_Recording::Example_Mass_Spring_Simluation_Recording(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags) {
	ui.setupUi(this);
	/*
	QGridLayout* gridLayout = new QGridLayout();
	gridLayout->setContentsMargins(2, 2, 2, 2);
	this->ui.viewportFrame->setLayout(gridLayout);

	this->openglContext = new Sx::OpenGL::Graphics::Contexts::OpenGL_Context();
	this->openglViewport = new Sx::QtInterface::QOpenGL_Viewport(nullptr, this->openglContext, 60);
	this->ui.viewportFrame->layout()->addWidget(this->openglViewport);
	this->openglViewport->setRealtime(true);
	//this->openglViewport->initialize();

	//--------------------------------------------------------------------------
	// Setup Collision.
	//--------------------------------------------------------------------------
	MSM_TS_CollisionModel* tscm = new MSM_TS_CollisionModel();
	EulerCollisionEventPoolAllocator<MassSpringMesh>* eulerAllocator = new EulerCollisionEventPoolAllocator<MassSpringMesh>();

	SelfCCD_MSM_CollisionDetectionHandler* ccd_msm_cdh = new SelfCCD_MSM_CollisionDetectionHandler();
	MSM_TS_InverseVelocityResponseHandler* msm_ts_ivrh = new MSM_TS_InverseVelocityResponseHandler();

	tscm->setCollisionDetectionHanlder(ccd_msm_cdh);
	tscm->setCollisionResponseHandler(msm_ts_ivrh);
	tscm->setAllocator(eulerAllocator);

	//--------------------------------------------------------------------------
	// Create a new mass-spring simulation.
	//--------------------------------------------------------------------------
	this->massSpringSimulation = new Sx::Simulation::Simulations::MassSpringSimulation(tscm);
	this->massSpringSimulation->setTimeStepType(Sx::Simulation::FIXED);
	this->massSpringSimulation->setTimeStep(1.0 / 1000.0f);
	this->massSpringSimulation->pause();

	//--------------------------------------------------------------------------
	// Setup a new mass-spring controller.
	// Controller ownership is transfered to the OpenGL Viewport.
	//--------------------------------------------------------------------------
	this->mssController = new MassSpringSimulationController(this->openglViewport, new OpenGL_DefaultCameraRenderer(this->openglContext), this->massSpringSimulation);
	this->mssController->initialize();

	//--------------------------------------------------------------------------
	// Create the demo objects based on the provided object files.
	//--------------------------------------------------------------------------
	this->object1 = Sx::Simulation::Objects::MassSpringMesh::Load(new Sx::Vega::Storage::OBJ_VegaMassSpringMeshStorage(), new Sx::Simulation::Objects::Rendering::OpenGL_DefaultMassSpringMeshRenderer(1.0f, 0.0f, 0.5f, openglContext), "t1.obj");
	this->object2 = Sx::Simulation::Objects::MassSpringMesh::Load(new Sx::Vega::Storage::OBJ_VegaMassSpringMeshStorage(), new Sx::Simulation::Objects::Rendering::OpenGL_DefaultMassSpringMeshRenderer(0.5f, 1.0f, 0.0f, openglContext), "t2.obj");
	this->object3 = Sx::Simulation::Objects::MassSpringMesh::Load(new Sx::Vega::Storage::OBJ_VegaMassSpringMeshStorage(), new Sx::Simulation::Objects::Rendering::OpenGL_DefaultMassSpringMeshRenderer(0.0f, 0.5f, 1.0f, openglContext), "t3.obj");
	this->ground = Sx::Simulation::Objects::MassSpringMesh::Load(new Sx::Vega::Storage::OBJ_VegaMassSpringMeshStorage(), new Sx::Simulation::Objects::Rendering::OpenGL_DefaultMassSpringMeshRenderer(0.2f, 0.2f, 0.4f, openglContext), "ground.obj");

	this->object1->setSubsteps(5);
	this->object2->setSubsteps(5);
	this->object3->setSubsteps(5);
	this->ground->setStatic(true);

	this->massSpringSimulation->add(this->ground);
	this->massSpringSimulation->add(this->object1);
	this->massSpringSimulation->add(this->object2);
	this->massSpringSimulation->add(this->object3);

	//--------------------------------------------------------------------------
	// Simulation Recording Initialization
	//--------------------------------------------------------------------------
	this->recorder = new Sx::Simulation::Recording::MSM_SimulationRecorder();
	this->recorder->setSimulation(this->massSpringSimulation);
	this->replayController = new MSS_ReplayController(this->openglViewport, new OpenGL_DefaultCameraRenderer(this->openglContext), this->massSpringSimulation);

	// Set the controller to be the mass-spring system
	this->openglViewport->setController(this->mssController);
	*/
}

Example_Mass_Spring_Simluation_Recording::~Example_Mass_Spring_Simluation_Recording() {
	//delete this->replayController;
	//delete this->recorder;
	//delete this->mssController;
	
	//delete this->massSpringSimulation;
	//delete this->openglViewport;
	//delete this->openglContext;
}

void Example_Mass_Spring_Simluation_Recording::resizeEvent(QResizeEvent* e) {
	//if ( this->openglViewport == nullptr ) return;
	//this->openglViewport->resize(this->width(), this->height());
}
/*
void Example_Mass_Spring_Simluation_Recording::onPauseResumeSimulation() {
	if ( this->massSpringSimulation == nullptr ) return;
}

void Example_Mass_Spring_Simluation_Recording::onResetSimulation() {
	if ( this->massSpringSimulation == nullptr ) return;
	this->massSpringSimulation->reset();
}

void Example_Mass_Spring_Simluation_Recording::onFPSChanged(int value) {
	if ( this->openglViewport == nullptr ) return;
	this->openglViewport->setFramesPerSecond(value);
}

void Example_Mass_Spring_Simluation_Recording::onPlayAndRecord() {
	if ( this->massSpringSimulation == nullptr ) return;
	if ( this->recorder == nullptr ) return;

	this->ui.playAndRecordButton->setEnabled(false);
	this->recorder->setRecording(true);
	this->massSpringSimulation->resume();
}

void Example_Mass_Spring_Simluation_Recording::onPlayRecording() {
	if ( this->massSpringSimulation == nullptr ) return;
	if ( this->openglViewport == nullptr ) return;

	this->ui.playAndRecordButton->setEnabled(false);
	this->replayController->setRecording(this->recorder->getRecording());
	this->replayController->setFrameIndex(0);
	this->replayController->setSimulation(this->massSpringSimulation);
	this->replayController->resume();
	this->replayController->resize(this->ui.viewportFrame->width(), this->ui.viewportFrame->height());
	this->openglViewport->setController(this->replayController);
}

void Example_Mass_Spring_Simluation_Recording::onStopRecording() {
	if ( this->massSpringSimulation == nullptr ) return;

	//this->ui.playAndRecordButton->setEnabled(true);
	this->recorder->setRecording(false);
	this->massSpringSimulation->pause();
}
*/
